from text import *
import os
import json
# 分数处理

def getScore() : # 从文件中读取分数信息
    try :
        fin = open("score.txt", "r")
        Score = [int(x) for x in fin.read().split()]
        fin.close()
    except :
        os.system(r"type nul > score.txt")
        Score = []
    return Score

def printScore(Score) : # 打印分数信息
    fin = open("score.txt", "w")
    for x in Score :
        fin.write(str(x) + ' ')
    fin.close

def saveScore(score) : # 保存页面
    Score = getScore()
    flag = False
    for x in Score :
        if score >= x :
            Score.insert(Score.index(x), score)
            flag = True
            break
    if not flag : Score.append(score)
    printScore(Score)

def scorePage(screen) : # 分数页面
    Author = TEXT("author : blankspace", AuthorColor, AuthorSize)

    screen.fill(backgroundColor)
    Author.printText(AuthorPosition, screen)

    Score = getScore()
    Len = len(Score)
    # 不存在分数信息
    if Len == 0 :
        screen.fill(backgroundColor)
        ERROR = TEXT("There is currently no score information available", ERRORColor, ERRORSize)
        ERROR.printText(ERRORPosition, screen)
        pygame.display.update()
        pygame.time.wait(3000)
        return
    # 打印排名
    scorePos = [[210, 180 + i * 50] for i in range(10)]
    def showScore(k) :
        if k == 0 : score = TEXT("NO." + str(k + 1) + " : " + str(Score[k]), scorePageFirstColor, scorePageTextSize)
        elif k == 1 : score = TEXT("NO." + str(k + 1) + " : " + str(Score[k]), scorePageSecondColor, scorePageTextSize)
        elif k == 2 : score = TEXT("NO." + str(k + 1) + " : " + str(Score[k]), scorePageThirdColor, scorePageTextSize)
        else : score = TEXT("NO." + str(k + 1) + " : " + str(Score[k]), scorePageTextColor, scorePageTextSize)
        score.printText(scorePos[k % 10], screen)

        pygame.display.update()
    # 标题及提示
    scorePageTitle = TEXT("Score ranking", scorePageTitleColor, scorePageTitleSize)
    scorePageTip = TEXT("Press the w/s or UP/DOWN key to change page ans press ENTER key to exit", scorePageTipColor, tipSize)
    # 页码
    nowPage = 0
    maxPage = Len // 10
    scorePageNumber = TEXT(str(nowPage + 1) + "/" + str(maxPage + 1), scorePageNumberColor, scorePageNumberSize)
    # 打印页面
    def showScorePage(l, r) :
        screen.fill(backgroundColor)
        Author.printText(AuthorPosition, screen)
        
        scorePageTitle.printText(scorePageTitlePosition, screen)
        scorePageTip.printText(tipPosition, screen)
        scorePageNumber.changeText(str(nowPage + 1) + "/" + str(maxPage + 1))
        scorePageNumber.printText(scorePageNumberPosition, screen)
        for i in range(l, r) : showScore(i)

    showScorePage(0, min(10, Len))
    # 键盘事件检测
    while True :
        for event in pygame.event.get() :
            if event.type == KEYDOWN :
                if event.key == K_UP or event.key == K_w :
                    Channel4.play(KeyBGM)
                    if nowPage > 0 :
                        nowPage -= 1
                        showScorePage(10 * nowPage, 10 * (nowPage + 1))
                if event.key == K_DOWN or event.key == K_s :
                    Channel4.play(KeyBGM)
                    if nowPage < maxPage :
                        nowPage += 1
                        showScorePage(10 * nowPage, min(10 * (nowPage + 1), Len))
                if event.key == K_RETURN :
                    Channel4.play(KeyBGM)
                    return
    